<template>
  <el-dialog title="" :model-value="modelValue" width="500px" @close="commEvent.onClose">
    <el-form ref="formRef" :model="formValue" :rules="formRules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formValue.name" placeholder="请输入名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="commEvent.onClose">取消</el-button>
      <el-button type="primary" @click="commEvent.onSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { nextTick, ref, watch } from "vue"
import { FormInstance, FormRules } from "element-plus"

const props = defineProps({
  modelValue: Boolean
})
const emit = defineEmits(["update:modelValue", "refresh"])
// 参数
const formRef = ref<FormInstance>() // 表单实例
// 表单参数
const formValue = ref({ name: "" })
// 表单校验
const formRules: FormRules = {
  name: [{ required: true, message: "请输入名称", trigger: "blur" }]
}
// 事件
const commEvent = {
  // 关闭弹窗
  onClose: () => {
    emit("update:modelValue", false)
  },
  // 提交表单
  onSubmit: () => {
    if (!formRef.value) return
    formRef.value.validate(async valid => {
      if (!valid) return
    })
  }
}
watch(
  () => props.modelValue,
  flag => {
    if (flag) {
      nextTick(() => {
        formRef.value?.resetFields()
      })
    }
  }
)
</script>

<style scoped></style>
